<template>
    <div>
        <el-form ref="loginForm"
                 v-loading="loading"
                 :rules="rules"
                 :model="loginForm"
                 class="loginContainer">
            <div class="login-mid">
                <div class="top">
                    <div class="title">
                        <span>欢迎使用</span>
                    </div>
                    <div class="desc">
                        <span>Bot Manager</span>
                    </div>
                </div>
                <div class="bottom">
                    <el-form-item prop="username">
                        <el-input type="text" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input type="password" placeholder="请输入密码" auto-complete="false" v-model="loginForm.password"></el-input>
                    </el-form-item>
                    <el-button class="btn-container" type="primary" style="width: 100%" @click.prevent="submitLogin">登录
                    </el-button>
                </div>
            </div>
            <div class="login-right">

            </div>

        </el-form>
    </div>
</template>

<script>
    export default {
        name: 'Login',
        data() {
            return {
                captchaUrl: '/api/captcha?time=' + new Date(),
                loginForm: {
                    username: '',
                    password: '',
                    code: ''
                },
                loading: false,
                checked: true,
                rules: {
                    username: [{
                        required: true,
                        message: '请输入用户名',
                        trigger: 'blur'
                    }],
                    password: [{
                        required: true,
                        message: '请输入密码',
                        trigger: 'blur'
                    }]
                }
            }
        },
        methods: {
            updateCaptcha() {
                this.captchaUrl = '/api/captcha?time=' + new Date()
            },
            submitLogin() {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.loading = true
                        this.postRequest('/login', this.loginForm).then(resp => {
                            if (resp) {
                                // 页面跳转
                                const path = this.$route.query.redirect
                                this.$router.replace((path === '/' || path === undefined) ? '/home' : path)
                                console.log(this.$router.currentRoute.value.name)
                            } else {
                                this.updateCaptcha()
                            }
                            this.loading = false
                        })
                    } else {
                        this.loading = false
                        this.$message.error('请输入所有字段')
                        return false
                    }
                })
            }
        }
    }
</script>

<style scoped>

    .loginContainer {
        margin: 100px auto;
        padding: 10px 40px 10px 40px;
        width: 400px;
        min-height: 400px;
        border-radius: 10px;
        background-color: white;
        box-shadow: 0 0 20px 5px rgba(34, 84, 142, .26);
    }

    .login-mid {
        width: 70%;
        margin: 0 auto;
    }

    .top {
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: flex-start;
        margin: 40px 0;
    }

    .title {
        font-size: 32px;
        margin-bottom: 16px;
    }

    .desc {
        font-size: 28px;
        text-align: left;
        color: rgb(166, 175, 188);
    }




</style>
